/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 21.01.2006
 *
 *************************************************/
package fightplan.ui;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import javax.swing.ComboBoxModel;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import fightplan.bindx.GObservableList;

public class GComboBoxModel<E> implements ComboBoxModel {
	private GObservableList<E> list;
	private Object selectedItem;
	protected EventListenerList listenerList = new EventListenerList();

	public GComboBoxModel(GObservableList<E> list) {
		this.list = list;
	}

	public Object getSelectedItem() {
		return selectedItem;
	}

	public void setSelectedItem(Object anItem) {
		if (anItem == selectedItem)
			return;
		selectedItem = anItem;
		fireChanged(this);
	}

	protected void fireChanged(Object source) {
		Object[] listeners = listenerList.getListenerList();
		ListDataEvent e = null;

		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == ListDataListener.class) {
				if (e == null) {
					e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, -1, -1);
				}
				((ListDataListener) listeners[i + 1]).contentsChanged(e);
			}
		}
	}

	// delegated methods

	public void add(int index, E element) {
		list.add(index, element);
	}

	public boolean add(E o) {
		return list.add(o);
	}

	public boolean addAll(Collection<E> c) {
		return list.addAll(c);
	}

	public boolean addAll(int index, Collection<E> c) {
		return list.addAll(index, c);
	}

	public void addListDataListener(ListDataListener l) {
		list.addListDataListener(l);
		listenerList.add(ListDataListener.class, l);
	}

	public void clear() {
		list.clear();
	}

	public boolean contains(Object o) {
		return list.contains(o);
	}

	public boolean containsAll(Collection<E> c) {
		return list.containsAll(c);
	}

	public boolean equals(Object o) {
		return list.equals(o);
	}

	public Object get(int index) {
		return list.get(index);
	}

	public Object getElementAt(int index) {
		return list.getElementAt(index);
	}

	public int getSize() {
		return list.getSize();
	}

	public int hashCode() {
		return list.hashCode();
	}

	public int indexOf(Object o) {
		return list.indexOf(o);
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public Iterator iterator() {
		return list.iterator();
	}

	public int lastIndexOf(Object o) {
		return list.lastIndexOf(o);
	}

	public ListIterator<E> listIterator() {
		return list.listIterator();
	}

	public ListIterator<E> listIterator(int index) {
		return list.listIterator(index);
	}

	public Object remove(int index) {
		return list.remove(index);
	}

	public boolean remove(Object o) {
		return list.remove(o);
	}

	public boolean removeAll(Collection<E> c) {
		return list.removeAll(c);
	}

	public void removeListDataListener(ListDataListener l) {
		list.removeListDataListener(l);
		listenerList.remove(ListDataListener.class, l);
	}

	public boolean retainAll(Collection<E> c) {
		return list.retainAll(c);
	}

	public Object set(int index, E element) {
		return list.set(index, element);
	}

	public int size() {
		return list.size();
	}

	public List subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}

	public Object[] toArray() {
		return list.toArray();
	}

	public Object[] toArray(Object[] a) {
		return list.toArray(a);
	}

}
